// 使用std::async向函数传参
#include <string>
#include <future>
struct X {
    void foo(int, std::string const&);
    std::string bar(std::string const&);
};
void test1() {
    X x;
    // calling p->foo(42, "hello")
    auto f1 = std::async(&X::foo, &x, 42, "hello");
    // calling tmpx.bar("goodbye"), tmpx是x的拷贝
    auto f2 = std::async(&X::bar, x, "goodbye");
}
struct Y {
    double operator()(double);
};
void test2() {
    X x;
    Y y;
    // calling tmpy(3.141)，tmpy由Y的移动构造函数构造
    auto f3 = std::async(Y(), 3.141);
    // calling y(2.718)
    auto f4 = std::async(std::ref(y), 2.718); 

    X baz(X&);
    std::async(baz, std::ref(x)); // calling baz(x)
}

class MoveOnly {
public:
    MoveOnly();
    MoveOnly(MoveOnly&&);
    MoveOnly(MoveOnly const&) = delete;
    MoveOnly& operator=(MoveOnly&&);
    MoveOnly& operator=(MoveOnly const&) = delete;

    void operator()();
};

void test3() {
    // calling std::move(MoveOnly())
    auto f5 = std::async(MoveOnly()); 
}

void test4() {
    X x;
    X baz(X&);
    // 在新的线程上执行
    auto f6 = std::async(std::launch::async, Y(), 1.2);
    // 在wait或get调用时执行
    auto f7 = std::async(std::launch::deferred, baz, std::ref(x));
    f7.wait(); //调用延时函数
    // 实现选择执行
    auto f8 = std::async(std::launch::deferred | std::launch::async, baz, std::ref(x));
    auto f9 = std::async(baz, std::ref(x));
}

